﻿using System;
using System.Text;
using System.Web.UI;
using System.Globalization;

namespace GoogleMaps
{
	/// <summary>
	/// Encapsulates a GEvent JavaScript object that will be used to respond to events fired by the 
	/// Google Map after it is rendered in the browser.
	/// </summary>
	[ParseChildren(true, "JavaScript"), 
	ControlValueProperty("JavaScript")]
	public class GEvent
	{
		private string javaScript;

		/// <summary>
		/// Renders the event by appending it to the stringbuilder that contains the JavaScript 
		/// for the related Google Map.
		/// </summary>
		/// <param name="js">A string builder containing the javascript that goes with the Google Map.</param>
		/// <param name="javaScriptVariableName">The namve of the JavaScript variable that is holding the map.</param>
		public void Render(StringBuilder js, String javaScriptVariableName)
		{
			js.AppendFormat(CultureInfo.InvariantCulture, "GEvent.addListener({0}, \"{1}\", function({2}) {{{3}}});",
				javaScriptVariableName, EventName, Parameters, JavaScript);
		}

		/// <summary>
		/// Gets or sets the name of the event this the listner is being added to.
		/// </summary>
		public string EventName { get; set; }

		/// <summary>
		/// Gets or sets the body of the function that is executed when the event is triggered.
		/// </summary>
		[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
		public string JavaScript
		{
			get { return javaScript.Trim(); }
			set { javaScript = value; }
		}

		/// <summary>
		/// Gets or sets a comma delimited list of javascript parameters to be used with the function
		/// </summary>
		public string Parameters { get;set;}
	}
}
